package database;

import java.sql.*;
import org.apache.log4j.Logger;

public class Database {

    static Logger logger = Logger.getLogger(Database.class);

    public static boolean checkUser(String appID) {
        Connection con = null;
        CallableStatement stmt = null;
        ResultSet res = null;
        try {
            con = MyConnection.getInstance().getConnection();
            try {
                stmt = con.prepareCall("call check_user(?,?,?)");
                stmt.setString(1, appID);
                stmt.registerOutParameter(2, Types.VARCHAR);
                stmt.registerOutParameter(3, Types.VARCHAR);
                stmt.execute();
                String msg = stmt.getString(3);
                if (!msg.equalsIgnoreCase("SUCCESS")) {
                } else {
                    String stat = stmt.getString(2);
                    if (stat.equalsIgnoreCase("FOUND")) {
                        return true;
                    }
                    return false;
                }
            } catch (Exception ex) {
                logger.fatal("DB query exception", ex);
            }

        } catch (Exception ex) {
            //connection exception
            ex.printStackTrace();
        } finally {

            MyConnection.close(con);
            MyConnection.close(stmt);
            MyConnection.close(res);

        }
        return false;
    }

    public static boolean checkSession(String sessionID) {
        Connection con = null;
        CallableStatement stmt = null;
        ResultSet res = null;
        try {
            con = MyConnection.getInstance().getConnection();
            try {
                stmt = con.prepareCall("call check_session(?,?,?)");
                stmt.setString(1, sessionID);
                stmt.registerOutParameter(2, Types.VARCHAR);
                stmt.registerOutParameter(3, Types.VARCHAR);
                stmt.execute();
                String msg = stmt.getString(3);
                if (!msg.equalsIgnoreCase("SUCCESS")) {
                } else {
                    String stat = stmt.getString(2);
                    if (stat.equalsIgnoreCase("active")) {
                        return true;
                    }
                    return false;
                }
            } catch (Exception ex) {
                logger.fatal("DB query exception");
            }

        } catch (Exception ex) {
            //connection exception
            ex.printStackTrace();
        } finally {

            MyConnection.close(con);
            MyConnection.close(stmt);
            MyConnection.close(res);

        }
        return false;
    }
}
